package cn.exrick.xboot.modules.masterdata.server.utils;

import java.util.*;

import cn.exrick.xboot.modules.masterdata.entity.MasterUser;
import cn.exrick.xboot.modules.masterdata.entity.dto.MasterUserDto;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.*;
@Slf4j
public class Dom4JforXML {
    /**
     * 对接主数据将xml报文转换成MasterUserDto
     *
     * @param data 主数据接口getData()接口返回的报文
     * @return
     */
    public static MasterUserDto xmlToMasterUserDto(String data) {
        MasterUserDto masterUserDto = new MasterUserDto();
        try {
            List<MasterUser> masterUserList = masterUserDto.getMasterUserList();
            String tableName = "";
            Document document = DocumentHelper.parseText(data);
            Element rootElement = document.getRootElement();// 获取根节点
            Map<String, Object> personMap = new HashMap<String, Object>();
            for (Iterator<?> iterator = rootElement.elementIterator(); iterator.hasNext(); ) {
                Element element = (Element) iterator.next();
                personMap.put(element.getName(), element.getTextTrim());// 将子节点的名称和值分别插入map中
                if ("datas".equals(element.getName())) {
                    for (Iterator<?> iteratorTwo = element.elementIterator(); iteratorTwo.hasNext(); ) {
                        //创建masterUser
                        MasterUser masterUser = new MasterUser();
                        Element elementTwo = (Element) iteratorTwo.next();
                        List<Attribute> attributes = elementTwo.attributes();
                        for (Attribute attribute : attributes) {
                            //System.out.println("属性" + attribute.getName() + ":" + attribute.getValue());
                            if ("tableName".equals(attribute.getName())) {
                                tableName = attribute.getValue();
                                masterUser.setTableName(tableName);
                            }
                            break;
                        }
                        //遍历对接用户数据
                        for (Iterator<?> iteratorThree = elementTwo.elementIterator(); iteratorThree.hasNext(); ) {
                            Element elementThree = (Element) iteratorThree.next();
                            List<Attribute> attributes1 = elementThree.attributes();
                            String name = elementThree.getName();
                            //通过标签名字筛选对应操作
                            switch (name) {
                                case "recid":
                                    masterUser.setRecid(elementThree.getTextTrim());
                                    break;
                                case "version":
                                    masterUser.setVersion(elementThree.getTextTrim());
                                    break;
                                case  "field":
                                    Attribute attribute = elementThree.attribute("name");
                                    Attribute attributeValue = elementThree.attribute("value");
                                    //System.out.println("属性" + attribute.getName() + ":" + attribute.getValue());
                                    if (attribute != null && attributeValue != null) {
                                        switch (attribute.getValue()) {
                                            case "STDCODE":
                                                masterUser.setIdcard(attributeValue.getValue());
                                                break;
                                            case "STDNAME":
                                                masterUser.setNickName(attributeValue.getValue());
                                                break;
                                            case "A0148":
                                                masterUser.setMobile(attributeValue.getValue());
                                                break;
                                            case "FLAG":
                                                masterUser.setFlag(attributeValue.getValue());
                                                break;
                                            case "A0107":
                                                masterUser.setSex(attributeValue.getValue());
                                                break;
                                        }
                                    }
                                    break;
                            }
                        }
                        masterUserList.add(masterUser);
                    }
                }
            }
            masterUserDto.setMasterUserList(masterUserList);
            masterUserDto.setTotalPage((String) personMap.get("totalPage"));
            masterUserDto.setCurrentPage((String) personMap.get("currentPage"));
            masterUserDto.setPageSize((String) personMap.get("pageSize"));
            masterUserDto.setTotalRecord((String) personMap.get("totalRecord"));
            masterUserDto.setTaskId((String) personMap.get("taskId"));

        } catch (Exception e) {
            log.error("解析报文出错");
            log.error("错误报文：data{}",data);
            e.printStackTrace();
        }
        return masterUserDto;
    }

    public static String getSendXml(MasterUserDto masterUserDto) {
        String head="<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                    +"<sendData> "
                    +"<taskId>"+masterUserDto.getTaskId()+"</taskId>"
                    +"<datas>";
        String datas="";
        String end= "</datas>"
                    +"</sendData>";
        //获取datas
        List<MasterUser> masterUserList = masterUserDto.getMasterUserList();
        datas = Dom4JforXML.getSendXmlDatas(masterUserList);
        String sendXml =head+datas+end;
        return sendXml;
    }
    public static String getSendXmlDatas( List<MasterUser> masterUserList ){
        StringBuffer datas=new StringBuffer();
        for (MasterUser masterUser : masterUserList) {
            String data="<data tableName="+'"'+masterUser.getTableName()+'"'+">"
                    +"<createUnit>--</createUnit>"
                    +"<recid>"+masterUser.getRecid()+"</recid>"
                    +"<version>"+masterUser.getVersion()+"</version>"
                    +"<sendDetail>成功</sendDetail>"
                    +"<sendState>1</sendState> "
                    +"</data>";
            datas.append(data);
        }
        return datas.toString();
    }


    public static void main(String[] args) throws Exception {
        String soapXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "\t<sendData>\n" +
                "\t\t<totalRecord>14183</totalRecord>\n" +
                "\t\t<totalPage>7092</totalPage>\n" +
                "\t\t<pageSize>2</pageSize>\n" +
                "\t\t<currentPage>1</currentPage>\n" +
                "\t\t<taskId>E406342F42EC3043F520E8805B2D427E</taskId>\n" +
                "\t\t<datas>\n" +
                "\t\t\t<data tableName=\"MD_MIDSTAFF\">\n" +
                "\t\t\t\t<createUnit>--</createUnit>\n" +
                "\t\t\t\t<recid>6EAFE23E60000081C2A633649E3F4761</recid>\n" +
                "\t\t\t\t<version>7975842222622900385</version>\n" +
                "\t\t\t\t<field name=\"STDCODE\" type=\"2\" value=\"430102196006032536\"/>\n" +
                "\t\t\t\t<field name=\"STDNAME\" type=\"2\" value=\"杨果义\"/>\n" +
                "\t\t\t\t<field name=\"A0177\" type=\"2\" value=\"430102196006032536\"/>\n" +
                "\t\t\t\t<field name=\"A0148\" type=\"2\" value=\"15874838130\"/>\n" +
                "\t\t\t\t<field name=\"LEVEL\" type=\"3\" value=\"1\"/>\n" +
                "\t\t\t\t<field name=\"ISLEAF\" type=\"4\" value=\"true\"/>\n" +
                "\t\t\t\t<field name=\"SORTORDER\" type=\"1\" value=\"3.0\"/>\n" +
                "\t\t\t\t<field name=\"INVALIDTIME\" type=\"6\" value=\"9999-12-31 00:00:00\"/>\n" +
                "\t\t\t\t<field name=\"VALIDTIME\" type=\"6\" value=\"1990-01-01 00:00:00\"/>\n" +
                "\t\t\t\t<field name=\"PARENTS\" type=\"10\"/>\n" +
                "\t\t\t</data>\n" +
                "\t\t\t<data tableName=\"MD_MIDSTAFF\">\n" +
                "\t\t\t\t<createUnit>--</createUnit>\n" +
                "\t\t\t\t<recid>6EAFE23E600000C1CFEBCB39884D8089</recid>\n" +
                "\t\t\t\t<version>7975842222622900449</version>\n" +
                "\t\t\t\t<field name=\"STDCODE\" type=\"2\" value=\"430111197402282685\"/>\n" +
                "\t\t\t\t<field name=\"STDNAME\" type=\"2\" value=\"朱月明\"/>\n" +
                "\t\t\t\t<field name=\"A0177\" type=\"2\" value=\"430111197402282685\"/>\n" +
                "\t\t\t\t<field name=\"A0148\" type=\"2\" value=\"13548705909\"/>\n" +
                "\t\t\t\t<field name=\"LEVEL\" type=\"3\" value=\"1\"/>\n" +
                "\t\t\t\t<field name=\"ISLEAF\" type=\"4\" value=\"true\"/>\n" +
                "\t\t\t\t<field name=\"SORTORDER\" type=\"1\" value=\"4.0\"/>\n" +
                "\t\t\t\t<field name=\"INVALIDTIME\" type=\"6\" value=\"9999-12-31 00:00:00\"/>\n" +
                "\t\t\t\t<field name=\"VALIDTIME\" type=\"6\" value=\"1990-01-01 00:00:00\"/>\n" +
                "\t\t\t\t<field name=\"PARENTS\" type=\"10\"/>\n" +
                "\t\t\t</data>\n" +
                "\t\t</datas>\n" +
                "\t</sendData> ";
        Dom4JforXML dom4JforXML = new Dom4JforXML();
        MasterUserDto masterUserDto = dom4JforXML.xmlToMasterUserDto(soapXML);
        String sendXml = dom4JforXML.getSendXml(masterUserDto);
        System.out.println(sendXml);
    }
}
